<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:productSelect:export']"
        >导出</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportExcel(1)"
          v-hasPermi="['purchase:productSelect:export']"
        >
          待采购接受统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportExcel(2)"
          v-hasPermi="['purchase:productSelect:export']"
        >
          请购-采购接收-入库明细统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportExcel(3)"
          v-hasPermi="['purchase:productSelect:export']"
        >
          接收物料待入库统计
        </el-button>
      </el-col>
      <Tag type="dot" color="#0fac09">已请购未接收</Tag>
      <Tag type="dot" color="#0323af">已接收未入库</Tag>
      <Tag type="dot" color="#bb0331">已入库未入完</Tag>
      <!--      <el-col :span="2.2" style="position: absolute;right: 35px;top:0">-->
      <!--        <Page :current="queryParams.pageNum" :page-size="queryParams.pageSize" :total="total"-->
      <!--              @on-change="pageChange" simple></Page>-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="物料代码" prop="productWldm">
            <el-input
              v-model="queryParams.productWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="营销单号" prop="productSodh">
            <el-input
              style="width: 240px"
              v-model="queryParams.productSodh"
              placeholder="请输入营销单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请购单号" prop="productQgdh">
            <el-input
              v-model="queryParams.productQgdh"
              placeholder="请输入请购单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购单号" prop="productCgdh">
            <el-input
              style="width: 240px"
              v-model="queryParams.productCgdh"
              placeholder="请输入采购单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库单号" prop="productRkdh">
            <el-input
              v-model="queryParams.productRkdh"
              placeholder="请输入入库单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="综合查询" prop="productPmgg">
            <el-input
              v-model="queryParams.productPmgg"
              placeholder="综合查询"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="请购日期">
            <el-date-picker
              v-model="daterangeProductQgdhTime"
              size="small"
              :clearable="false"
              style="width: 400px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 子表TAB-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="待采购接收统计" name="first">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="pendingList"
          border
          stripe
          id="table1"
          @scrollBottom="load2"
          :height="tableH"
          @selection-change="handleSelectionChange"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="请购单号"
            :show-overflow-tooltip="true"
            align="center"
            prop="productQgdh"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            align="center"
            prop="productWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            align="center"
            prop="productPmgg"
          />
          <el-table-column
            label="客户编码"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCustCode"
          />
          <el-table-column
            label="物料厂商"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCsdm"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="productDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.productDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购属性"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCgsx"
          >
            <template #default="scope">
              <dict-tag
                :options="bomCgsxOptions"
                :value="scope.row.productCgsx"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            :show-overflow-tooltip="true"
            align="center"
            prop="productMinBz"
          />
          <el-table-column
            label="库存数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productKcsl"
          />
          <el-table-column
            label="请购总数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productQgTotal"
          />
          <el-table-column
            label="待采购总数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCgTotal"
            width="180"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="请购-采购接收-入库明细统计" name="second">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="productSelectList"
          border
          stripe
          id="table2"
          :cell-style="iCellStyle"
          @scrollBottom="load"
          :height="tableH"
          @selection-change="handleSelectionChange"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="55" align="center" />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="140"
            align="center"
            prop="productWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="productPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="productDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.productDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="营销单号"
            :show-overflow-tooltip="true"
            width="120"
            align="center"
            prop="productSodh"
          />
          <el-table-column
            label="订单数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productSodhNumber"
          />
          <el-table-column
            label="请购单号"
            :show-overflow-tooltip="true"
            width="130"
            align="center"
            prop="productQgdh"
          />
          <el-table-column
            label="请购数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productQgdhNumber"
          />
          <el-table-column
            label="采购单号"
            :show-overflow-tooltip="true"
            width="130"
            align="center"
            prop="productCgdh"
          />
          <el-table-column
            label="采购数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCgdhNumber"
          />
          <el-table-column
            label="入库单号"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="productRkdh"
          />
          <el-table-column
            label="入库数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productRkdhNumber"
          />
          <el-table-column
            label="厂商"
            :show-overflow-tooltip="true"
            align="center"
            prop="productRkdhCsdm"
          >
            <template #default="scope">
              <dict-tag
                :options="csOptions"
                :value="scope.row.productRkdhCsdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="请购日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="productQgdhTime"
            width="150"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.productQgdhTime, "{y}-{m}-{d}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="采购日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCgdhTime"
            width="150"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.productCgdhTime, "{y}-{m}-{d}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="入库日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="productRkdhTime"
            width="150"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.productRkdhTime, "{y}-{m}-{d}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="接收物料待入库统计" name="third">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="productOfRkingList"
          border
          stripe
          id="table3"
          @scrollBottom="load3"
          :height="tableH"
          @selection-change="handleSelectionChange"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="55" align="center" />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            align="center"
            prop="productWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            align="center"
            prop="productPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="productDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.productDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="库存数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="productKcsl"
          />
          <el-table-column
            label="采购接收数"
            :show-overflow-tooltip="true"
            align="center"
            prop="productCgNumber"
          />
          <el-table-column
            label="已入库数"
            :show-overflow-tooltip="true"
            align="center"
            prop="productAlreadyRk"
          />
          <el-table-column
            label="结案扣减数"
            :show-overflow-tooltip="true"
            align="center"
            prop="productOverNumber"
          />
          <el-table-column
            label="待入库数"
            :show-overflow-tooltip="true"
            align="center"
            prop="productWaitRk"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  listProductSelect,
  getProductSelect,
  delProductSelect,
  addProductSelect,
  updateProductSelect,
  exportProductSelect,
} from "@/api/purchase/productSelect";
import { getAllCgList } from "@/api/project/mstr";
import { getCs } from "@/api/putStorage/putStorageAdd";
import XLSX from "xlsx";
import FileSaver from "file-saver";

export default {
  name: "ProductSelect",
  data() {
    return {
      activeTab: "second",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      bomCgsxOptions: [],

      //厂商
      csOptions: [],

      itmDwOptions: [],
      // 总条数
      total: 0,

      // 待入库
      rkTotal: 0,
      tableH: document.body.clientHeight - 300,
      //待采购总条数
      pendingTotal: 0,

      //待采购
      pendingList: [],
      // 待生产采购明细表格数据
      productSelectList: [],
      //待入库
      productOfRkingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 请购日期时间范围
      // daterangeProductQgdhTime: [],
      daterangeProductQgdhTime: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        // pageNum2: 1,
        // pageNum3: 1,
        pageSize: 30,
        productWldm: null,
        productSodh: null,
        productQgdh: null,
        productCgdh: null,
        productRkdh: null,
        productPmgg: null,
        productQgdhTime: null,
        index: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    getAllCgList().then((response) => {
      this.bomCgsxOptions = response.data;
      for (let i of this.bomCgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });

    getCs().then((response) => {
      this.csOptions = response.data;
      for (let item of this.csOptions) {
        item.dictLabel = item.csmCsmc;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
  },
  methods: {
    iCellStyle({ row, column, rowIndex, columnIndex }) {
      //请购状态
      if (row.productCgdh == null && row.productRkdh == null) {
        return "color:#0fac09";
      }
      //采购状态
      else if (
        row.productQgdh != null &&
        row.productCgdh != null &&
        row.productRkdh == null
      ) {
        return "color:#0323af";
      } else if (row.productRkdh != null) {
        return "color:#bb0331";
      }
    },

    /** 分页跳转 */
    // pageChange(page){
    //   this.queryParams.pageNum = page;
    //   this.getList();
    // },
    /** 查询待生产采购明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      // this.queryParams2.params = {};
      // this.queryParams.params = {};
      this.queryParams.pageNum = 1;
      // this.queryParams2.pageNum = 1
      // this.queryParams.pageNum = 1
      if (
        null != this.daterangeProductQgdhTime &&
        "" != this.daterangeProductQgdhTime
      ) {
        this.queryParams.params["beginProductQgdhTime"] = this.moment(
          this.daterangeProductQgdhTime[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endProductQgdhTime"] = this.moment(
          this.daterangeProductQgdhTime[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listProductSelect(this.queryParams).then((response) => {
        console.log(response);
        this.pendingList = this.pendingList.concat(
          response.data.pendingList.rows
        );
        // this.pendingList = response.data.pendingList.rows;
        this.pendingTotal = response.data.pendingList.total;

        this.productSelectList = this.productSelectList.concat(
          response.data.list.rows
        );
        // this.productSelectList = response.data.list.rows;
        this.total = response.data.list.total;
        this.productOfRkingList = this.productOfRkingList.concat(
          response.data.productOfRkingList.rows
        );
        // this.productOfRkingList = response.data.productOfRkingList.rows;
        this.rkTotal = response.data.list.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listProductSelect(this.queryParams).then((response) => {
          this.pendingList = this.pendingList.concat(
            response.data.pendingList.rows
          );
          // this.pendingList = response.data.pendingList.rows;
          this.pendingTotal = response.data.pendingList.total;

          this.productSelectList = this.productSelectList.concat(
            response.data.list.rows
          );
          // this.productSelectList = response.data.list.rows;
          this.total = response.data.list.total;
          this.productOfRkingList = this.productOfRkingList.concat(
            response.data.productOfRkingList.rows
          );
          // this.productOfRkingList = response.data.productOfRkingList.rows;
          this.rkTotal = response.data.list.total;

          this.loading = false;
        });
      }
      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.pendingTotal / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listProductSelect(this.queryParams).then((response) => {
          this.pendingList = this.pendingList.concat(
            response.data.pendingList.rows
          );
          // this.pendingList = response.data.pendingList.rows;
          this.pendingTotal = response.data.pendingList.total;

          this.productSelectList = this.productSelectList.concat(
            response.data.list.rows
          );
          // this.productSelectList = response.data.list.rows;
          this.total = response.data.list.total;
          this.productOfRkingList = this.productOfRkingList.concat(
            response.data.productOfRkingList.rows
          );
          // this.productOfRkingList = response.data.productOfRkingList.rows;
          this.rkTotal = response.data.list.total;

          this.loading = false;
        });
      }
      return 0;
    },
    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.rkTotal / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listProductSelect(this.queryParams).then((response) => {
          this.pendingList = this.pendingList.concat(
            response.data.pendingList.rows
          );
          // this.pendingList = response.data.pendingList.rows;
          this.pendingTotal = response.data.pendingList.total;

          this.productSelectList = this.productSelectList.concat(
            response.data.list.rows
          );
          // this.productSelectList = response.data.list.rows;
          this.total = response.data.list.total;
          this.productOfRkingList = this.productOfRkingList.concat(
            response.data.productOfRkingList.rows
          );
          // this.productOfRkingList = response.data.productOfRkingList.rows;
          this.rkTotal = response.data.list.total;

          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productSodh: null,
        productSodhNumber: null,
        productQgdh: null,
        productQgdhNumber: null,
        productCgdh: null,
        productCgdhNumber: null,
        productRkdh: null,
        productRkdhNumber: null,
        productRkdhCsdm: null,
        productQgdhTime: null,
        productCgdhTime: null,
        productRkdhTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pendingList = [];
      this.productSelectList = [];
      this.productOfRkingList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProductQgdhTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.productId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待生产采购明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids;
      getProductSelect(productId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改待生产采购明细";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询待生产采购明细";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.productId != null) {
            updateProductSelect(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductSelect(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$confirm(
        '是否确认删除待生产采购明细编号为"' + productIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delProductSelect(productIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {},
    exportExcel(e) {
      this.queryParams.index = e;
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportProductSelect(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
